<?php
/**
 * User: Mykola B.
 * Date: 3/2/13
 * Time: 8:18 AM
 */
namespace models {
    class UserProfileModel extends BaseModel
    {

        protected static $fields = array(
            'id',
            'user_id',
            'key',
            'value',
            'type'
        );

        public function __construct() {
            return parent::__construct('user_profiles');
        }

        public function getByKeyForUser($userId, $key) {
            $this->mapper->load(array('`key`=? AND user_id=?', $key, $userId));
            return $this->mapper;
        }

        public function fillObject(UserModel $user, $key, $value, $type='string') {
            // load it first to overwrite later
            $this->mapper->load(array('`key`=?', $key));

            $this->mapper->user_id = $user->id;
            $this->mapper->key = $key;
            $this->mapper->value = $value;
            switch(strtolower($type)) {
                case 'int':
                    $this->mapper->type = 0;
                    break;
                case 'string':
                    $this->mapper->type = 1;
                    break;
                case 'bool':
                    $this->mapper->type = 2;
                    break;
                case 'json':
                    $this->mapper->type = 3;
                    break;
                default:
                    $this->mapper->type = null;
            }
        }
    }
}
